package hikvision

import (
	"encoding/xml"
	"errors"
)

type isApi struct {
}

func (e *isApi) QueryCapabilities(ip string) (*DeviceCap, error) {
	// 发出请求
	resp, err := GET("http://"+ip, "/ISAPI/System/capabilities")
	if err != nil {
		return nil, err
	}

	// 尝试使用DeviceCap解码数据
	var deviceCap DeviceCap
	err = xml.Unmarshal(resp, &deviceCap)
	if err == nil {
		return &deviceCap, nil
	}

	// 尝试使用userCheck解码数据
	var userCheck UserCheck
	err = xml.Unmarshal(resp, &userCheck)
	if err != nil {
		return nil, errors.New("无法解析返回的数据")
	}

	return nil, errors.New(userCheck.StatusString)
}

func (e *isApi) Login(host string) error {
	return tryLogin(host)
}
